package com.southminority.ethnic.controller.alioss;



import com.southminority.ethnic.common.result.R;
import com.southminority.ethnic.controller.alioss.vo.OssFileReqVo;
import com.southminority.ethnic.service.alioss.OssService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Tag(name = "阿里oss存储",description = "oss存储接口")
@RestController
@RequestMapping("/aliyunOss")
@CrossOrigin
public class OssController {
    @Autowired
    OssService ossService;
    @Operation(summary = "根据文件分类上传文件")
    @PostMapping
    public R<String> upload(@Validated OssFileReqVo ossFileReqVo) {
        try{
            return R.ok(ossService.upload(ossFileReqVo.getFile(),ossFileReqVo.getClassName()));
        } catch (Exception e) {
            return R.fail(e.getMessage());
        }

    }

    /*@Operation(summary = "根据文件分类更新文件")
    @PostMapping("/updateFile")
    public String updateFile(@Validated OssFileUpdataReqVo ossFileUpdataReqVo) {
        try {
            return ossService.updateFile(ossFileUpdataReqVo.getFile(),ossFileUpdataReqVo.getUrl());
        }catch (Exception e){
            e.printStackTrace();
            return "更新失败";
        }

    }*/
    
    /**
     * 测试OSS服务状态
     */
    @Operation(summary = "测试OSS服务状态")
    @PostMapping("/test")
    public R<String> testOssService() {
        try {
            boolean isConfigured = ossService.testOssConfig();
            if (isConfigured) {
                return R.ok("OSS服务配置检查通过，可以正常使用文件上传功能");
            } else {
                return R.fail("OSS服务配置错误，请在application.yml中配置真实的阿里云OSS参数。当前配置为占位符配置，无法进行文件上传。");
            }
        } catch (Exception e) {
            return R.fail("OSS服务配置错误: " + e.getMessage());
        }
    }
}
